﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Net;
using Blue.Spider.Core.Entities;
using Blue.Spider.Core.Extensions;

namespace Blue.Spider.Core
{
    public class SpiderEngine
    {
        public string StartUrl { get; set; }
        public ListPageBase ListPage { get; set; }

        public void Start()
        {
            WebClient client = new WebClient();

            string s = client.DownloadString(StartUrl);

            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(s);

            HtmlNode ListContainer = this.GetListContainerElement(document);

            List<DetailPageBase> items = new List<DetailPageBase>();

            //go on each html list element
            foreach (HtmlNode ListItem in ListContainer.Elements("li").Where(i => i.Attributes["class"].Value.Contains("box-aviso-sumario")))
            {
                DetailPageBase detail = new DetailPageBase();
                detail.Load(ListItem);

                items.Add(detail);
            }

        }

        private HtmlNode GetListContainerElement(HtmlDocument document)
        {
            //return document.GetElementbyId("resultadoBusqueda").Elements("ul").Where(i => i.Attributes["class"].Value.Contains("box-avisos-listado")).First();
            return document.GetElementbyId("resultadoBusqueda").ElementByClass("ul", "box-avisos-listado").First();
        }


    }
}
